package com.wxl.common.wiget

import android.content.Context
import android.view.KeyEvent
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.CenterPopupView
import com.wxl.common.R
import com.wxl.common.base.AbsActivity
import io.reactivex.functions.Action
import kotlinx.android.synthetic.main.pop_warn_layout.view.*

class WarnPop : CenterPopupView{

    var text:String = ""
    var action:Action?=null
    var action0:Action?=null

    constructor(context: Context) : super(context)

    override fun getImplLayoutId(): Int {
        return R.layout.pop_warn_layout
    }

    override fun onCreate() {
        super.onCreate()
        warnPopText.text = text
        warnPopSure.setOnClickListener {
            dismiss()
            action?.run()
        }
        warnPopCancel.setOnClickListener {
            dismiss()
            action0?.run()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_F1){
            warnPopCancel.performClick()
        }
        if(keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_F4){
            warnPopSure.performClick()
        }
        return true
    }

    companion object {
        fun show(context: Context,text:String,action:Action?,action0:Action?){
            val p = WarnPop(context)
            p.action = action
            p.action0 = action0
            (context as AbsActivity).addPop(p)
            p.text = text
            XPopup.Builder(context)
                .hasShadowBg(false)
                .dismissOnTouchOutside(false)
                .dismissOnBackPressed(false)
                .asCustom(p)
                .show()
        }
    }
}